package com.jr.Controller;

import com.google.gson.Gson;
import com.jr.Pojo.Packages;
import com.jr.Service.IUsersService;
import com.jr.Service.Impl.UsersServiceImpl;
import com.jr.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/users/*")
public class UsersController extends HttpServlet {
    IUsersService ius = new UsersServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        int index = requestURI.lastIndexOf("/")+1;
        String mName=requestURI.substring(index);
        Class cla = this.getClass();
        Method method=null;
        try{
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            resp.setContentType("application/json;charset=utf-8");
            req.setCharacterEncoding("utf-8");
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void showdt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String id = req.getParameter("id");
        String receiver = req.getParameter("receiver");
        PageHelper ph=new PageHelper();
        ph.setDateCount(ius.queryAllDataCount(id,receiver));
        ph.setPageIndex(Integer.parseInt(req.getParameter("indexpage")));
        ArrayList<Packages> packages = ius.queryAll(id,receiver,ph);
        ph.setListIndex(packages);
        resp.getWriter().println(new Gson().toJson(ph));
    }
}
